package com.example.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.R;
import com.example.util.DisplayUtil;
import com.example.util.QRCodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 *分享页面Activity
 */
public class SharePageActivity extends AppCompatActivity {
    @BindView(R.id.img_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.relay_contain)
    RelativeLayout reyContainer;

    private Intent intent;
    private Bitmap shareBitmap;

    private String qrUrl = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_page);
        ButterKnife.bind(this);
        initView();
        createQrCode();
    }

    /**
     *
     */
    private void initView(){
        intent = getIntent();
        String qrCode = intent.getStringExtra("code");
        String url = intent.getStringExtra("url");
        if(!TextUtils.isEmpty(qrCode)){
            tvInviteCode.setText(qrCode);
            qrUrl = url;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    /**
     *
     */
    private void createQrCode(){
        int width = (int) DisplayUtil.Dp2Px(this, 90);
        int height = (int) DisplayUtil.Dp2Px(this, 90);
        String character_set = "UTF-8";
        String error_correction_level = "H";
        String margin = "1";
        int color_black = Color.BLACK;
        int color_white = Color.WHITE;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.outWidth = (int) DisplayUtil.Dp2Px(this, 21);
        options.outHeight = (int) DisplayUtil.Dp2Px(this, 21);
        Bitmap logoBitmap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher_round, options);
        float logoPercent = 0.2F;
        Bitmap bm = QRCodeUtils.createQRCodeBitmap(qrUrl, width, height, character_set,
                error_correction_level, margin, color_black, color_white, logoBitmap, logoPercent);

        ivQrCode.setImageBitmap(bm);
    }



    @OnClick({R.id.tv_save,R.id.iv_back})
    public void onClickView(View view){
        switch (view.getId()){
            case R.id.tv_save:
                Toast.makeText(this,"保存中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
